package com.itdianzi.listener;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import com.itdianzi.export_common.common.utils.MailUtil;

//实现监听器接口  MessageListener 监听器接口
public class MyListener implements MessageListener{
    //定义转换json的接口
    private static final ObjectMapper MAPPER = new ObjectMapper();

    //监听方法
    public void onMessage(Message message) {

        try {
            //其实相当于map
            JsonNode jsonNode = MAPPER.readTree(message.getBody());

            String to = jsonNode.get("to").asText().replaceAll("\"" , "");//获得数据并转换成文本
            String content = jsonNode.get("content").asText().replaceAll("\"" , "");//获得数据并转换成文本
            String subject = jsonNode.get("subject").asText().replaceAll("\"" , "");//获得数据并转换成文本
            MailUtil.sendMsg(to , subject , content);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
